

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>kospeech.model_builder &mdash; KoSpeech 0.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> KoSpeech
          

          
          </a>

          
            
            
              <div class="version">
                0.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">NOTES</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../notes/intro.html">Intro</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/Preparation.html">Preparation before Training</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notes/opts.html">Options</a></li>
</ul>
<p class="caption"><span class="caption-text">ARCHITECTURE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../Seq2seq.html">Seq2seq</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Transformer.html">Transformer</a></li>
</ul>
<p class="caption"><span class="caption-text">PACKAGE REFERENCE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../Checkpoint.html">Checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Data.html">Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Decode.html">Decode</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Evaluator.html">Evaluator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Optim.html">Optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Trainer.html">Trainer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Etc.html">Etc</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">KoSpeech</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>kospeech.model_builder</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for kospeech.model_builder</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="kn">from</span> <span class="nn">astropy.modeling</span> <span class="k">import</span> <span class="n">ParameterError</span>
<span class="kn">from</span> <span class="nn">kospeech.decode.ensemble</span> <span class="k">import</span> <span class="n">BasicEnsemble</span><span class="p">,</span> <span class="n">WeightedEnsemble</span>
<span class="kn">from</span> <span class="nn">kospeech.models.seq2seq.sublayers</span> <span class="k">import</span> <span class="n">BaseRNN</span>
<span class="kn">from</span> <span class="nn">kospeech.models.seq2seq.seq2seq</span> <span class="k">import</span> <span class="n">Seq2seq</span>
<span class="kn">from</span> <span class="nn">kospeech.models.seq2seq.encoder</span> <span class="k">import</span> <span class="n">Seq2seqEncoder</span>
<span class="kn">from</span> <span class="nn">kospeech.models.seq2seq.decoder</span> <span class="k">import</span> <span class="n">Seq2seqDecoder</span>
<span class="kn">from</span> <span class="nn">kospeech.models.transformer.transformer</span> <span class="k">import</span> <span class="n">Transformer</span>
<span class="kn">from</span> <span class="nn">kospeech.utils</span> <span class="k">import</span> <span class="n">char2id</span><span class="p">,</span> <span class="n">EOS_token</span><span class="p">,</span> <span class="n">SOS_token</span><span class="p">,</span> <span class="n">PAD_token</span>


<div class="viewcode-block" id="build_model"><a class="viewcode-back" href="../../Etc.html#kospeech.model_builder.build_model">[docs]</a><span class="k">def</span> <span class="nf">build_model</span><span class="p">(</span><span class="n">opt</span><span class="p">,</span> <span class="n">device</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Various model dispatcher function. &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">opt</span><span class="o">.</span><span class="n">transform_method</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;spect&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">opt</span><span class="o">.</span><span class="n">feature_extract_by</span> <span class="o">==</span> <span class="s1">&#39;kaldi&#39;</span><span class="p">:</span>
            <span class="n">input_size</span> <span class="o">=</span> <span class="mi">257</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">input_size</span> <span class="o">=</span> <span class="p">(</span><span class="n">opt</span><span class="o">.</span><span class="n">frame_length</span> <span class="o">&lt;&lt;</span> <span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">input_size</span> <span class="o">=</span> <span class="n">opt</span><span class="o">.</span><span class="n">n_mels</span>

    <span class="k">if</span> <span class="n">opt</span><span class="o">.</span><span class="n">architecture</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;seq2seq&#39;</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">build_seq2seq</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">device</span><span class="p">)</span>

    <span class="k">elif</span> <span class="n">opt</span><span class="o">.</span><span class="n">architecture</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;transformer&#39;</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">build_transformer</span><span class="p">(</span><span class="n">num_classes</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">num_classes</span><span class="p">,</span> <span class="n">pad_id</span><span class="o">=</span><span class="n">PAD_token</span><span class="p">,</span> <span class="n">input_size</span><span class="o">=</span><span class="n">input_size</span><span class="p">,</span>
                                  <span class="n">d_model</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">d_model</span><span class="p">,</span> <span class="n">num_heads</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">num_heads</span><span class="p">,</span> <span class="n">eos_id</span><span class="o">=</span><span class="n">EOS_token</span><span class="p">,</span>
                                  <span class="n">num_encoder_layers</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">num_encoder_layers</span><span class="p">,</span> <span class="n">num_decoder_layers</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">num_decoder_layers</span><span class="p">,</span>
                                  <span class="n">dropout_p</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">dropout</span><span class="p">,</span> <span class="n">ffnet_style</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">ffnet_style</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unsupported architecture: </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">opt</span><span class="o">.</span><span class="n">architecture</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">model</span></div>


<span class="k">def</span> <span class="nf">build_transformer</span><span class="p">(</span><span class="n">num_classes</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">pad_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">d_model</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">num_heads</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">input_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
                      <span class="n">num_encoder_layers</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">num_decoder_layers</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
                      <span class="n">dropout_p</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">ffnet_style</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">device</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Transformer</span><span class="p">:</span>
    <span class="k">if</span> <span class="n">ffnet_style</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">{</span><span class="s1">&#39;ff&#39;</span><span class="p">,</span> <span class="s1">&#39;conv&#39;</span><span class="p">}:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;Unsupported ffnet_style: </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ffnet_style</span><span class="p">))</span>

    <span class="n">model</span> <span class="o">=</span> <span class="n">Transformer</span><span class="p">(</span><span class="n">num_classes</span><span class="o">=</span><span class="n">num_classes</span><span class="p">,</span> <span class="n">pad_id</span><span class="o">=</span><span class="n">pad_id</span><span class="p">,</span> <span class="n">d_model</span><span class="o">=</span><span class="n">d_model</span><span class="p">,</span> <span class="n">num_heads</span><span class="o">=</span><span class="n">num_heads</span><span class="p">,</span>
                        <span class="n">num_encoder_layers</span><span class="o">=</span><span class="n">num_encoder_layers</span><span class="p">,</span> <span class="n">num_decoder_layers</span><span class="o">=</span><span class="n">num_decoder_layers</span><span class="p">,</span>
                        <span class="n">dropout_p</span><span class="o">=</span><span class="n">dropout_p</span><span class="p">,</span> <span class="n">ffnet_style</span><span class="o">=</span><span class="n">ffnet_style</span><span class="p">,</span> <span class="n">input_dim</span><span class="o">=</span><span class="n">input_size</span><span class="p">,</span> <span class="n">eos_id</span><span class="o">=</span><span class="n">eos_id</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">nn</span><span class="o">.</span><span class="n">DataParallel</span><span class="p">(</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>


<div class="viewcode-block" id="build_seq2seq"><a class="viewcode-back" href="../../Etc.html#kospeech.model_builder.build_seq2seq">[docs]</a><span class="k">def</span> <span class="nf">build_seq2seq</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">device</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Various Listen, Attend and Spell dispatcher function. &quot;&quot;&quot;</span>
    <span class="n">encoder</span> <span class="o">=</span> <span class="n">build_seq2seq_encoder</span><span class="p">(</span><span class="n">input_size</span><span class="o">=</span><span class="n">input_size</span><span class="p">,</span> <span class="n">hidden_dim</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">hidden_dim</span><span class="p">,</span> <span class="n">dropout_p</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">dropout</span><span class="p">,</span>
                                    <span class="n">num_layers</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">num_encoder_layers</span><span class="p">,</span> <span class="n">bidirectional</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">use_bidirectional</span><span class="p">,</span>
                                    <span class="n">extractor</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">extractor</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">activation</span><span class="p">,</span>
                                    <span class="n">rnn_type</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">rnn_type</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span> <span class="n">mask_conv</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">mask_conv</span><span class="p">)</span>
    <span class="n">decoder</span> <span class="o">=</span> <span class="n">build_seq2seq_decoder</span><span class="p">(</span><span class="n">num_classes</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">char2id</span><span class="p">),</span> <span class="n">max_len</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">max_len</span><span class="p">,</span>
                                    <span class="n">sos_id</span><span class="o">=</span><span class="n">SOS_token</span><span class="p">,</span> <span class="n">eos_id</span><span class="o">=</span><span class="n">EOS_token</span><span class="p">,</span>
                                    <span class="n">hidden_dim</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">hidden_dim</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="mi">1</span> <span class="k">if</span> <span class="n">opt</span><span class="o">.</span><span class="n">use_bidirectional</span> <span class="k">else</span> <span class="mi">0</span><span class="p">),</span>
                                    <span class="n">num_layers</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">num_decoder_layers</span><span class="p">,</span> <span class="n">rnn_type</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">rnn_type</span><span class="p">,</span> <span class="n">dropout_p</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">dropout</span><span class="p">,</span>
                                    <span class="n">num_heads</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">num_heads</span><span class="p">,</span> <span class="n">attn_mechanism</span><span class="o">=</span><span class="n">opt</span><span class="o">.</span><span class="n">attn_mechanism</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">)</span>

    <span class="n">model</span> <span class="o">=</span> <span class="n">Seq2seq</span><span class="p">(</span><span class="n">encoder</span><span class="p">,</span> <span class="n">decoder</span><span class="p">)</span>
    <span class="n">model</span><span class="o">.</span><span class="n">flatten_parameters</span><span class="p">()</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">DataParallel</span><span class="p">(</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">model</span></div>


<div class="viewcode-block" id="build_seq2seq_encoder"><a class="viewcode-back" href="../../Etc.html#kospeech.model_builder.build_seq2seq_encoder">[docs]</a><span class="k">def</span> <span class="nf">build_seq2seq_encoder</span><span class="p">(</span><span class="n">input_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">hidden_dim</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">dropout_p</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
                          <span class="n">num_layers</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">bidirectional</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
                          <span class="n">rnn_type</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">extractor</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
                          <span class="n">activation</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">device</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">mask_conv</span><span class="p">:</span> <span class="nb">bool</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Seq2seqEncoder</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot; Various encoder dispatcher function. &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">dropout_p</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;dropout probability should be positive&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">input_size</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;input_size should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">hidden_dim</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;hidden_dim should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">num_layers</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;num_layers should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">extractor</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">{</span><span class="s1">&#39;vgg&#39;</span><span class="p">,</span> <span class="s1">&#39;ds2&#39;</span><span class="p">}:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;Unsupported extractor&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">extractor</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">rnn_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">BaseRNN</span><span class="o">.</span><span class="n">supported_rnns</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;Unsupported RNN Cell: </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">rnn_type</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">Seq2seqEncoder</span><span class="p">(</span><span class="n">input_size</span><span class="o">=</span><span class="n">input_size</span><span class="p">,</span> <span class="n">hidden_dim</span><span class="o">=</span><span class="n">hidden_dim</span><span class="p">,</span>
                          <span class="n">dropout_p</span><span class="o">=</span><span class="n">dropout_p</span><span class="p">,</span> <span class="n">num_layers</span><span class="o">=</span><span class="n">num_layers</span><span class="p">,</span> <span class="n">mask_conv</span><span class="o">=</span><span class="n">mask_conv</span><span class="p">,</span>
                          <span class="n">bidirectional</span><span class="o">=</span><span class="n">bidirectional</span><span class="p">,</span> <span class="n">rnn_type</span><span class="o">=</span><span class="n">rnn_type</span><span class="p">,</span>
                          <span class="n">extractor</span><span class="o">=</span><span class="n">extractor</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">)</span></div>


<div class="viewcode-block" id="build_seq2seq_decoder"><a class="viewcode-back" href="../../Etc.html#kospeech.model_builder.build_seq2seq_decoder">[docs]</a><span class="k">def</span> <span class="nf">build_seq2seq_decoder</span><span class="p">(</span><span class="n">num_classes</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">max_len</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">hidden_dim</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
                          <span class="n">sos_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">eos_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">attn_mechanism</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">num_layers</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
                          <span class="n">rnn_type</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">dropout_p</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">num_heads</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">device</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Seq2seqDecoder</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot; Various decoder dispatcher function. &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">hidden_dim</span> <span class="o">%</span> <span class="n">num_heads</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{0}</span><span class="s2"> % </span><span class="si">{1}</span><span class="s2"> should be zero&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">hidden_dim</span><span class="p">,</span> <span class="n">num_heads</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">dropout_p</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;dropout probability should be positive&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">num_heads</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;num_heads should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">hidden_dim</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;hidden_dim should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">num_layers</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;num_layers should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">max_len</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;max_len should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">num_classes</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;num_classes should be greater than 0&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">rnn_type</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">BaseRNN</span><span class="o">.</span><span class="n">supported_rnns</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;Unsupported RNN Cell: </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">rnn_type</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">device</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ParameterError</span><span class="p">(</span><span class="s2">&quot;device is None&quot;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">Seq2seqDecoder</span><span class="p">(</span><span class="n">num_classes</span><span class="o">=</span><span class="n">num_classes</span><span class="p">,</span> <span class="n">max_length</span><span class="o">=</span><span class="n">max_len</span><span class="p">,</span>
                          <span class="n">hidden_dim</span><span class="o">=</span><span class="n">hidden_dim</span><span class="p">,</span> <span class="n">sos_id</span><span class="o">=</span><span class="n">sos_id</span><span class="p">,</span> <span class="n">eos_id</span><span class="o">=</span><span class="n">eos_id</span><span class="p">,</span>
                          <span class="n">attn_mechanism</span><span class="o">=</span><span class="n">attn_mechanism</span><span class="p">,</span> <span class="n">num_heads</span><span class="o">=</span><span class="n">num_heads</span><span class="p">,</span>
                          <span class="n">num_layers</span><span class="o">=</span><span class="n">num_layers</span><span class="p">,</span> <span class="n">rnn_type</span><span class="o">=</span><span class="n">rnn_type</span><span class="p">,</span>
                          <span class="n">dropout_p</span><span class="o">=</span><span class="n">dropout_p</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">)</span></div>


<span class="k">def</span> <span class="nf">load_test_model</span><span class="p">(</span><span class="n">opt</span><span class="p">,</span> <span class="n">device</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">opt</span><span class="o">.</span><span class="n">model_path</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="k">lambda</span> <span class="n">storage</span><span class="p">,</span> <span class="n">loc</span><span class="p">:</span> <span class="n">storage</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>

    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DataParallel</span><span class="p">):</span>
        <span class="n">model</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">device</span>
        <span class="n">model</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">device</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="n">model</span><span class="o">.</span><span class="n">encoder</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">device</span>
        <span class="n">model</span><span class="o">.</span><span class="n">decoder</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">device</span>

    <span class="k">return</span> <span class="n">model</span>


<span class="k">def</span> <span class="nf">load_language_model</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">device</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="k">lambda</span> <span class="n">storage</span><span class="p">,</span> <span class="n">loc</span><span class="p">:</span> <span class="n">storage</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>

    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DataParallel</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">module</span>

    <span class="n">model</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">device</span>

    <span class="k">return</span> <span class="n">model</span>


<span class="k">def</span> <span class="nf">build_ensemble</span><span class="p">(</span><span class="n">model_paths</span><span class="p">,</span> <span class="n">method</span><span class="p">,</span> <span class="n">device</span><span class="p">):</span>
    <span class="n">models</span> <span class="o">=</span> <span class="nb">list</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">model_path</span> <span class="ow">in</span> <span class="n">model_paths</span><span class="p">:</span>
        <span class="n">models</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">model_path</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="k">lambda</span> <span class="n">storage</span><span class="p">,</span> <span class="n">loc</span><span class="p">:</span> <span class="n">storage</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;basic&#39;</span><span class="p">:</span>
        <span class="n">ensemble</span> <span class="o">=</span> <span class="n">BasicEnsemble</span><span class="p">(</span><span class="n">models</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;weight&#39;</span><span class="p">:</span>
        <span class="n">ensemble</span> <span class="o">=</span> <span class="n">WeightedEnsemble</span><span class="p">(</span><span class="n">models</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported ensemble method : </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">method</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">ensemble</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Soohwan Kim

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>